package com.cloudkinto.service.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.entity.PlatformStoreDo;
import com.cloudkinto.service.consumer.vo.markShip.Qoo10MarkShipConsumerReq;
import com.cloudkinto.service.consumer.vo.orderSyncRecord.PlatformOrderSyncRecordReq;
import com.cloudkinto.service.consumer.vo.rakuten.RakutenOrderUpdateConReq;
import com.cloudkinto.service.consumer.vo.yahoo.*;
import com.cloudkinto.service.platform.PlatformOrderYahooSyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformOrderYahooConsumer {

    private final PlatformOrderYahooSyncService platformOrderYahooSyncOrderService;
    private final RabbitTemplate rabbitTemplate;


    /**
     * Yahoo用户授权 获取到刷新令牌后， 去获取她的 sellerId
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Yahoo_UserInfo))
    public void getYahooUserInfoApi(String msg) {
        Map<String, Object> object = JSONObject.parseObject(msg);
        Object shopId = object.get("shopId");
        if (shopId == null){
            System.out.println("Yahoo用户授权 获取到刷新令牌后， 去获取她的 sellerId 无 shopId");
            return;
        }
        System.out.println("yahoo.getYahooUserInfoApi交换机1消费 ," + object.get("shopName") + ":::店铺 获取sellerId");
        try {
            platformOrderYahooSyncOrderService.getYahooUserInfoApi(Long.parseLong(shopId.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("yahoo.getYahooUserInfoApi交换机1消费 ," + object.get("shopName") + "::: 店铺 获取sellerId");
    }


    /**
     * 同步订单表历史
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Labella_PlatformStore_Yahoo_History_Sync))
    public void syncOrderYahooHistory(String msg) {
        YahooConsumerReq req = JSONObject.parseObject(msg, YahooConsumerReq.class);
        try {
            String s = platformOrderYahooSyncOrderService.updateYahooOrdersHistory(req);
            if (StringUtils.isNotBlank(s)) {
                log.error(s);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 同步订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Labella_PlatformStore_Yahoo_Sync))
    public void syncOrderRakuten(String msg) {
        YahooConsumerReq req = JSONObject.parseObject(msg, YahooConsumerReq.class);
        String s = "";
        try {
            s = platformOrderYahooSyncOrderService.updateYahooOrders(req);
        }catch (Exception e) {
            s = e.getMessage();
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(s)) {
            rabbitTemplate.convertAndSend(RabbitQueueConstant.PlatformOrder_SyncRecord_Update, JSON.toJSONString(new PlatformOrderSyncRecordReq(req.getSyncId(), 3, new Date(), null, s, null)));
        }
    }



    /**
     * 更新乐天订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Yahoo_Update))
    public void syncOrderRakutenUpdate(String msg) {
        RakutenOrderUpdateConReq req = JSONObject.parseObject(msg, RakutenOrderUpdateConReq.class);
        try {
            platformOrderYahooSyncOrderService.consumerUpdate(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 标发
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Yahoo_MarkShip))
    public void markShip(String msg) {
        YahooMarkShipConsumerReq req = JSONObject.parseObject(msg, YahooMarkShipConsumerReq.class);
        try {
            platformOrderYahooSyncOrderService.makeShip(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 截单后，标记订单已读
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Yahoo_MarkSeen))
    public void markSeen(String msg) {
        log.info(msg);
        YahooMarkStatusConsumer req = JSONObject.parseObject(msg, YahooMarkStatusConsumer.class);
        try {
            platformOrderYahooSyncOrderService.makeSeen(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 标发后，标记订单完成
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Yahoo_MarkStatus))
    public void markFinish(String msg) {
        log.info(msg);
        YahooMarkStatusConsumer req = JSONObject.parseObject(msg, YahooMarkStatusConsumer.class);
        try {
            platformOrderYahooSyncOrderService.makeFinish(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 库存回传
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_Yahoo))
    public void setStock(String msg) {
        YahooStockToPlatReq req = JSONObject.parseObject(msg, YahooStockToPlatReq.class);
        try {
            platformOrderYahooSyncOrderService.setStock(req.getSkuSetStockList(), req.getStoreDo(), req.getUserId());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 同步商品 获取itemCode
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.GetPlatformProduct_ItemList_Yahoo))
    public void getItemList(String msg) {
        PlatformStoreDo req = JSONObject.parseObject(msg, PlatformStoreDo.class);
        try {
            platformOrderYahooSyncOrderService.getItemList(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 同步商品标 根据itemCode
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.GetPlatformProduct_Yahoo))
    public void getItem(String msg) {
        YahooGetItemReq req = JSONObject.parseObject(msg, YahooGetItemReq.class);
        try {
            platformOrderYahooSyncOrderService.getItem(req.getItemCode(), req.getStoreDo());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
